# -*- coding: utf-8 -*-
"""
    201901, Dr. Jie Zheng, Beijing & Xinglong, NAOC
    202101-? Dr. Jie Zheng & Dr./Prof. Linqiao Jiang
    Light_Curve_Pipeline
    v3 (2021A) Upgrade from former version, remove unused code
"""


import os
import numpy as np
import astropy.io.fits as fits
from .JZ_utils import loadlist, datestr, logfile, conf, meanclip, hdr_dt_jd
# from .JZ_cata import match
# from .JZ_trimatch import make_tri, match_triangle, argunique
from .JZ_match import offset1d, match2d

def _offset_(ini, basef, catf, base_img_id, base_cat_file, out_offset_file, lf):
    """

    :param ini:
    :param basef:
    :param catf:
    :param base_img_id:
    :param base_cat_file:
    :param out_offset_file:
    :param lf:
    :return:
    """

    nf = len(catf)
    lf.show("{:03d} files".format(nf), logfile.DEBUG)

    # prepare an empty catalog cube
    xy_off = np.zeros((6, nf))
    n_off = np.zeros(nf, dtype=int)
    r_off = np.zeros(nf, dtype=int)
    jd = np.zeros(nf)

    # base catalog
    # cat_base = fits.getdata(catf[base_img_id], 2) 200118
    # 200118 add extra template catalog, can be a catalog not in the list
    base_cat_file = base_cat_file if base_img_id < 0 else catf[base_img_id]
    if not os.path.isfile(base_cat_file):
        raise FileExistsError("Base catalog {} NOT exists".format(base_cat_file))

    cat_base = fits.getdata(base_cat_file)
    # nx = fits.getval(catf[0], "IMNAXIS1")
    # ny = fits.getval(catf[0], "IMNAXIS2")
    nx = fits.getval(base_cat_file, "IMNAXIS1")
    ny = fits.getval(base_cat_file, "IMNAXIS2")
    border_cut = ini["border_cut"]
    # 190609 remove border stars
    x_base = cat_base["X"]  # [ini["se_x"]]
    y_base = cat_base["Y"]  # [ini["se_y"]]
    cat_base = cat_base[
        (border_cut < x_base) & (x_base < nx - border_cut) &
        (border_cut < y_base) & (y_base < ny - border_cut)
    ]

    # goodix = np.where(cat_base[ini["se_err"]] < ini["offset_err_limit"])[0]
    # 190604 use brightest 10 stars
    # goodix = np.argsort(cat_base[ini["se_mag"]])[:ini["offset_count"]]
    goodix = np.argsort(cat_base["Mag"])[:ini["offset_count"]]
    x_base = cat_base[goodix]["X"]    # [ini["se_x"]]
    y_base = cat_base[goodix]["Y"]    # [ini["se_y"]]
    m_base = cat_base[goodix]["Mag"]  # [ini["se_mag"]]

    lf.show("Load {:03d}/{:03d}: N={:4d}/{:4d} Base Image {}".format(
        base_img_id, nf, len(goodix), len(cat_base), catf[base_img_id]), logfile.DEBUG)

    # load images and process
    for f in range(nf):
        # jd[f] = hdr_dt_jd(fits.getheader(catf[f]), ini) - 2458000.0
        jd[f] = fits.getval(catf[f], "MJD") - 58000
        if f != base_img_id:
            # load n_th catalog
            cat_k = fits.getdata(catf[f])
            # 190609 remove border stars
            x_k = cat_k["X"]  # [ini["se_x"]]
            y_k = cat_k["Y"]  # [ini["se_y"]]
            cat_k = cat_k[
                (border_cut < x_k) & (x_k < nx - border_cut) &
                (border_cut < y_k) & (y_k < ny - border_cut)
                ]
            goodix = np.argsort(cat_k["Mag"])[:ini["offset_count"]]
            # goodix = np.argsort(cat_k[ini["se_mag"]])[:ini["offset_count"]]
            x_k = cat_k[goodix]["X"]    # [ini["se_x"]]
            y_k = cat_k[goodix]["Y"]    # [ini["se_y"]]
            m_k = cat_k[goodix]["Mag"]  # [ini["se_mag"]]
            lf.show("Load {:3d}/{:3d}: N={:4d}/{:4d} {}".format(
                f, nf, len(goodix), len(cat_k), catf[f]), logfile.DEBUG)

            # offset by 1d
            dxm, dxs = offset1d(x_base, x_k)
            dym, dys = offset1d(y_base, y_k)

            # match and compute offset, mag diff
            ix_b, ix_k = match2d(x_base, y_base, x_k + dxm, y_k + dym, ini["cali_dis_limit"])
            dx = x_base[ix_b] - x_k[ix_k]
            dy = y_base[ix_b] - y_k[ix_k]
            dm = m_base[ix_b] - m_k[ix_k]
            dr = np.sqrt(dx * dx + dy * dy)
            dxm, dxs = meanclip(dx, func=np.mean)
            dym, dys = meanclip(dy, func=np.mean)
            dmm, dms = meanclip(dm)
            drm, drs = meanclip(dr)
            n = len(ix_b)

            xy_off[:, f] = dxm, dxs, dym, dys, dmm, dms
            n_off[f] = n
            r_off[f] = 1 # fixed

    with open(out_offset_file, "w") as ff:
        ff.write("{:3} {:20} {:4} {:2} {:8} {:7}  {:8} {:7}  {:7} {:7}  {:10}\n".format(
            "No", "Filename", "Cnt", "It", "X_Med", "X_Std", "Y_Med", "Y_Std", "Mag_Med", "Mag_Std", "MJD"
        ))
        for f in range(nf):
            ff.write("{:3d} {:20s} {:4d} {:2d} {:+8.3f} {:7.4f}  {:+8.3f} {:7.4f}  {:+7.3f} {:7.4f}  {:10.6f}\n".format(
                f, basef[f], n_off[f], r_off[f],
                xy_off[0, f], xy_off[1, f],
                xy_off[2, f], xy_off[3, f],
                xy_off[4, f], xy_off[5, f],
                jd[f],
            ))
    lf.show("Report save to {}".format(out_offset_file), logfile.INFO)
